<template>
  <div class="alar-box">
    <div class="add-user-btn" @click="openSnapModel(1)">
      <span>新增摄像头</span>
    </div>
    <el-table
        max-height="734"
        :data="tableData"
        style="
        width: 100%;
        margin-top: 12px;
        background-color: #303136;
        color: #ffff;
      "
        :border="false"
    >
      <!--      todo  摄像头 区域 绑定预警类型 管理 -->
      <el-table-column prop="cameraName" label="摄像头名称"></el-table-column>
      <el-table-column prop="cameraIp" label="ip"></el-table-column>
      <el-table-column prop="pullUrl" label="识别地址"></el-table-column>
      <el-table-column prop="pushUrl" label="播放地址"></el-table-column>
      <el-table-column prop="position" label="区域"></el-table-column>
      <el-table-column prop="ruleText" label="预警规则">
        <template slot-scope="scope">
          <div v-html="scope.row.ruleText"></div>

        </template>
      </el-table-column>
      <el-table-column prop="opration" label="操作">
        <template slot-scope="scope">
          <span class="opration-btn" @click="openSnapModel(2,scope.row)">编辑摄像头</span>
          <span class="opration-btn" @click="openSnapWarnModel(scope.row)">绑定预警规则</span>
          <!--           <span class="opration-btn" @click="delCamera(scope.row)">删除摄像头</span>-->
        </template>
      </el-table-column>
    </el-table>

    <div class="table-pagination">
      <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNo"
          :page-sizes="pageSizeArr"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalCount"
      >
      </el-pagination>
    </div>
    <el-dialog
        :close-on-click-modal=false
        :modal-append-to-body="false"
        :title="dialogTitle"
        :visible.sync="visible"
        width="35%"
    >
      <camera-form
          v-if="visible"
          :modeType="modeType"
          :snapUserInfo="currentUserInfo"
          @onClose="closeModel"
          @onUpdate="onUpdate"
      ></camera-form
      >
    </el-dialog>
    <el-dialog
        :close-on-click-modal=false
        :modal-append-to-body="false"
        title="设置预警规则"
        :visible.sync="setWarnTypeVisible"
        width="35%"
    >
      <select-warn-type
          :bindInfo = "bindInfo"
          @onClose="closeModel"
          @onUpdate="onUpdate"
      ></select-warn-type>
    </el-dialog>
  </div>
</template>

<script>
import CameraForm from "./components/CameraForm.vue";
import SelectWarnType from "./components/SelectWarnType.vue";
import {delCamera, getCameraList} from "@/api/camera";

export default {
  components: {
    CameraForm,
    SelectWarnType
  },
  data() {
    return {
      bindInfo:{},
      radio1: 1,
      setWarnTypeVisible:false,
      visible: false,
      dialogTitle: "新增摄像头",
      modeType: 0,
      currentUserInfo: {},

      tableData: [],
      pageNo: 1,
      pageSize: 10,
      pageSizeArr: [10, 30, 50],
      totalCount: 0,
    };
  },
  mounted() {
    this.getCameraList();
  },
  methods: {
    closeModel(e) {
      this.visible = false;
      this.setWarnTypeVisible=false
      this.bindInfo = [];
      this.currentUserInfo = [];
      if (e.isClose) {
        this.tableData = [];
        this.pageNo = 1;
        this.getCameraList();
      }
    },
    openSnapModel(type, data = {}) {
      this.visible = true;
      this.modeType = type;
      if (type == 1) {
        this.dialogTitle = "新增摄像头信息";
      } else {
        this.dialogTitle = "编辑摄像头信息";
        this.currentUserInfo = data;
      }
    },
    openSnapWarnModel(data) {
      this.setWarnTypeVisible = true;
      this.bindInfo = data
    },
    onUpdate() {
      window.location.reload()
    },
    async getCameraList() {
      let res = await getCameraList({
        pageNo: this.pageNo,
        pageSize: this.pageSize,
      });

      if (res.code == 200) {
        res.data.lists.forEach((item) => {
          item.isWarn = item.isWarning == 1 ? true : false;
        });
        this.tableData = Array.from(
            new Set([...this.tableData, ...res.data.lists])
        );

        this.totalCount = res.data.count;
      }
    },

    //删除人员库
    async delCamera(data) {
      let res = await delCamera({id: data.id});
      if (res.code == 200) {
        this.$message({
          message: "删除成功",
          type: "success",
        });

        this.tableData = [];
        this.pageNo = 1;
        await this.getCameraList();
      }
    },

    handleSizeChange(e) {
      this.pageSize = e;
      this.pageNo = 1;
      this.tableData = [];
      this.getCameraList();
    },
    handleCurrentChange(e) {
      this.pageNo = e;
      this.tableData = [];
      this.getCameraList();
    },
    handleClick(scope) {
      console.log(scope, "tablescope");
    },

  },
};
</script>

<style lang="scss" scoped>
.add-user-btn {
  width: 118px;
  height: 36px;
  text-align: center;
  line-height: 36px;
  background: #5cfbff;
  color: #17181A;
  border-radius: 4px;
  cursor: pointer;
}

.opration-btn {
  display: inline-block;
  color: #fff;
  background: #40414d;
  border-radius: 4px;
  cursor: pointer;
  padding: 6px 12px;
  margin-right: 16px;
  margin-top: 16px;
}

.level-color {
  display: inline-block;
  width: 16px;
  height: 16px;
}

.table-pagination {
  text-align: right;
  margin: 30px 0;
}

.left20 {
  margin-left: 20px;
}
</style>

<style>
.el-radio__label {
  padding-left: 5px;
}

.el-radio__inner {
  background: transparent;
  border: 1px solid #fff !important;
}

.el-radio {
  color: #fff !important;
}

.el-radio__input.is-checked .el-radio__inner {
  background-color: #5cfbff;
}

.el-radio__input.is-checked + .el-radio__label {
  color: #5cfbff;
}
</style>
